package com.jp.knowledge.util;


import android.content.Context;
import android.util.Log;

import com.jp.knowledge.model.IModel;
import com.jp.knowledge.model.NetLogModel;

import java.util.Date;

import rx.Subscriber;


/**
 * @Data: 16/4/12 下午4:17
 * @Description: 自定义调阅者以及结果监听接口
 */
public class ResultSubscriber extends Subscriber<IModel> {
    private int mRequestType;
    private OnResultListener mListener = null;
    private Context mContext;

    private HttpErrorHand  httpErrorHand;

    public void setHttpErrorHand(HttpErrorHand httpErrorHand) {
        this.httpErrorHand = httpErrorHand;
    }

    /**
     * 自定义订阅，参数用来区分网络接口，以用来在不同接口操作过程中，处理不同的逻辑
     *
     * @param
     */


    public ResultSubscriber(Context context) {
        mContext = context;
        mListener = new OnResultListener() {
            @Override
            public void onStart(int requestType) {
            }

            @Override
            public void onCompleted(int requestType) {
            }

            @Override
            public void onError(int requestType) {
            }

            @Override
            public void onNext(IModel data, int requestType) {
            }
        };
    }

    @Override
    public void onStart() {
        mListener.onStart(mRequestType);
    }

    @Override
    public void onCompleted() {
        mListener.onCompleted(mRequestType);
    }

    @Override
    public void onError(Throwable e) {
        try {
            if (e != null) {
                Log.e("ResultSubscriber","onError====>"+ e.getMessage()+"===error_code=");
//                ToasUtil.toast(context, "网络异常，请稍后再试");
                mListener.onError(mRequestType);

                NetLogModel netLogModel = new NetLogModel();
                netLogModel.setType("Android");
                netLogModel.setRequestTime(TimeUtil.dateFormater.get().format(new Date(System.currentTimeMillis())));
                netLogModel.setResponseTime(netLogModel.getRequestTime());
                netLogModel.setMessage(e.getMessage());
                NetLogUtil.saveNetMsgToLocal(netLogModel);

                e.printStackTrace();
            }
        } catch (Throwable t) {
            t.printStackTrace();
        }

    }

    @Override
    public void onNext(IModel data) {
//            String str =JsonUtil.getInstance().objToJson(data);
//        L.i(str);
////        if(data.getErrcode()==0)
        mListener.onNext(data, mRequestType);
//        else
//        try {
//            ToasUtil.toast(context,data.getMessage());
//        }catch (Exception e){
//
//        }

    }

    /**
     * 设置订阅监听器
     *
     * @param listener
     */
    public void setOnResultListener(OnResultListener listener) {
        if (listener != null) {
            mListener = listener;
        }
    }

    /**
     * 设置请求接口类型
     *
     * @param requestType
     */
    public void setRequestType(int requestType) {
        this.mRequestType = requestType;
    }

    /**
     * 订阅的监听器
     */
    public interface OnResultListener {
        /**
         * 网络请求订阅开始
         */
        void onStart(int requestType);

        /**
         * 网络请求完成
         */
        void onCompleted(int requestType);

        /**
         * 网络请求错误
         */
        void onError(int requestType);

        /**
         * 处理请求结果
         */
        void onNext(IModel data, int requestType);
    }


    public abstract  class HttpErrorHand{
        public void done(IModel model){

        }
    }
}
